package com.blockchain.console.service.financial.achieve;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.blockchain.common.enums.IsPass;
import com.blockchain.common.enums.PtXlbType;
import com.blockchain.common.enums.XlbType;
import com.blockchain.common.enums.XlbZcStatus;
import com.blockchain.common.enums.ZrType;
import com.blockchain.console.service.financial.BManage;
import com.blockchain.console.service.financial.entity.BEntity;
import com.blockchain.console.service.financial.entity.TtQbEntity;
import com.blockchain.console.service.financial.query.BQuery;
import com.blockchain.framework.config.ConfigureProvider;
import com.blockchain.framework.data.sql.SQLConnectionProvider;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.ArrayParser;
import com.blockchain.framework.service.query.ItemParser;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.EmailUtil;
import com.blockchain.util.Formater;
import com.blockchain.util.StringHelper;
import com.blockchain.util.parser.DateTimeParser;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.variables.P2PConst;
import com.blockchain.variables.defines.SystemVariable;

public class BManageImpl extends AbstractFinancialService implements BManage {

	public static class FinancialManageFactory implements ServiceFactory<BManage> {

		@Override
		public BManage newInstance(ServiceResource serviceResource) {
			return new BManageImpl(serviceResource);
		}
	}

	public BManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}

	@Override
	public PagingResult<BEntity> searchBzr(BQuery query, Paging paging) throws Throwable {
		ArrayList<Object> parameters = new ArrayList<Object>();
		StringBuffer s = new StringBuffer("SELECT T6010.F01 userid,T6010.F02 username,T6013.F03 ywm,T6013.F04 zwm,");
		s.append(
				" T6027.F04 zrsl,T6027.F05 sjdz,T6027.F06 zc,T6027.F07 sj,T6027.F08 zr,T6027.F09 sfcg,T6027.F01 id FROM T6027 ");
		s.append(" LEFT JOIN T6010 ON T6027.F02=T6010.F01");
		s.append(" LEFT JOIN T6013 ON T6027.F03=T6013.F01 WHERE 1=1");
		if (query != null) {
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
			if (query.getUserid() > 0) {
				s.append(" AND T6027.F02=?");
				parameters.add(query.getUserid());
			}
			if (query.getBid() > 0) {
				s.append(" AND T6027.F03=?");
				parameters.add(query.getBid());
			}
			if (query.getZt() != null) {
				s.append(" AND T6027.F09=?");
				parameters.add(query.getZt());
			}

			if (query.getStartExtractionTime() != null) {
				s.append(" AND DATE(T6027.F07)>=?");
				parameters.add(query.getStartExtractionTime());
			}

			if (query.getEndExtractionTime() != null) {
				s.append(" AND DATE(T6027.F07)<=?");
				parameters.add(query.getEndExtractionTime());
			}
			if (!StringHelper.isEmpty(query.getYhm())) {
				s.append(" AND T6010.F02 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(query.getYhm()));
			}

		}
		s.append(" ORDER BY T6027.F07 DESC");
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<BEntity>() {
			ArrayList<BEntity> list = new ArrayList<BEntity>();

			@Override
			public BEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					BEntity b = new BEntity();
					b.userid = re.getInt(1);
					b.username = re.getString(2);
					b.b_ywm = re.getString(3);
					b.b_zwm = re.getString(4);
					b.zrsl = re.getBigDecimal(5);
					b.sjdz = re.getBigDecimal(6);
					b.zr_zcdz = re.getString(7);
					b.sj = re.getTimestamp(8);
					b.zr_zrdz = re.getString(9);
					b.zt = EnumParser.parse(ZrType.class, re.getString(10));
					b.id = re.getInt(11);
					list.add(b);
				}
				return list.toArray(new BEntity[list.size()]);
			}
		}, paging, s.toString(), parameters);
	}

	@Override
	public PagingResult<BEntity> searchBzc(BQuery query, Paging paging) throws Throwable {
		ArrayList<Object> parameters = new ArrayList<Object>();
		StringBuffer s = new StringBuffer(
				"SELECT T6010.F01 userid,T6010.F02 username,T6013.F03 ywm,T6013.F04 zwm,T6012.F05 dz,");
		s.append(
				" T6028.F05 zcsl,T6028.F06 sxf,T6028.F07 sj,T6028.F08 sfcg,T6028.F01 id,T6028.F10 zczt,T6028.F12 shsj,T6028.F14 zcshsj,T6028.F15 bd FROM T6028 ");
		s.append(" LEFT JOIN T6010 ON T6028.F02=T6010.F01");
		s.append(" LEFT JOIN T6013 ON T6028.F03=T6013.F01");
		s.append(" LEFT JOIN T6012 ON T6028.F04=T6012.F01 WHERE 1=1");
		if (query != null) {
			SQLConnectionProvider sqlConnectionProvider = getSQLConnectionProvider();
			if (query.getUserid() > 0) {
				s.append(" AND T6028.F02=?");
				parameters.add(query.getUserid());
			}
			if (query.getBid() > 0) {
				s.append(" AND T6028.F03=?");
				parameters.add(query.getBid());
			}
			if (query.getZt() != null) {
				s.append(" AND T6028.F09=?");
				parameters.add(query.getZt());
			}

			if (query.getStartExtractionTime() != null) {
				s.append(" AND DATE(T6028.F07)>=?");
				parameters.add(query.getStartExtractionTime());
			}

			if (query.getEndExtractionTime() != null) {
				s.append(" AND DATE(T6028.F07)<=?");
				parameters.add(query.getEndExtractionTime());
			}
			if (!StringHelper.isEmpty(query.getYhm())) {
				s.append(" AND T6010.F02 LIKE ?");
				parameters.add(sqlConnectionProvider.allMatch(query.getYhm()));
			}
			if (query.getXlbZt() != null) {
				if(query.getXlbZt() == XlbZcStatus.ZCCG){
					s.append(" AND T6028.F10=? OR T6028.F10=?");
					parameters.add(XlbZcStatus.QZZC);
					parameters.add(query.getXlbZt());
				} else {
					s.append(" AND T6028.F10=?");
					parameters.add(query.getXlbZt());
				}
			}

		}
		s.append(" ORDER BY T6028.F07 DESC");
		return selectPaging(getConnection(P2PConst.DB_USER), new ArrayParser<BEntity>() {
			ArrayList<BEntity> list = new ArrayList<BEntity>();

			@Override
			public BEntity[] parse(ResultSet re) throws SQLException {
				while (re.next()) {
					BEntity b = new BEntity();
					b.userid = re.getInt(1);
					b.username = re.getString(2);
					b.b_ywm = re.getString(3);
					b.b_zwm = re.getString(4);
					b.zcdz = re.getString(5);
					b.zcsl = re.getBigDecimal(6);
					b.sxf = re.getBigDecimal(7);
					b.sj = re.getTimestamp(8);
					b.zt = EnumParser.parse(ZrType.class, re.getString(9));
					b.id = re.getInt(10);
					b.zczt = EnumParser.parse(XlbZcStatus.class, re.getString(11));
					b.shsj = re.getTimestamp(12);
					b.zcsj = re.getTimestamp(13);
					b.sfbd=EnumParser.parse(IsPass.class, re.getString(14));
					b.sfyc=getHx(b.id);
					list.add(b);
				}
				return list.toArray(new BEntity[list.size()]);
			}
		}, paging, s.toString(), parameters);
	}

	private String getHx(int id)throws SQLException {
		return selectString(P2PConst.DB_USER,"SELECT F16 FROM T6028 WHERE F01=? AND F10=? ",id,XlbZcStatus.ZCCG);
	}
	
	@Override
	public BEntity getBzc(int id) throws Throwable {
		StringBuffer s = new StringBuffer(
				"SELECT T6010.F01 userid,T6010.F02 username,T6013.F03 ywm,T6013.F04 zwm,T6012.F05 dz,");
		s.append(
				" T6028.F05 zcsl,T6028.F06 sxf,T6028.F07 sj,T6028.F08 sfcg,T6028.F01 id,T6028.F12 shsj,T6013.F18 ip,T6013.F01 bid,T6013.F23 methodId,T6013.F24  contractAddress ,T6013.F25 unit,T6013.F26 cointype FROM T6028 ");
		s.append(" LEFT JOIN T6010 ON T6028.F02=T6010.F01");
		s.append(" LEFT JOIN T6013 ON T6028.F03=T6013.F01");
		s.append(" LEFT JOIN T6012 ON T6028.F04=T6012.F01 WHERE T6028.F01=?");
		return select(getConnection(P2PConst.DB_USER), new ItemParser<BEntity>() {
			BEntity b = new BEntity();

			@Override
			public BEntity parse(ResultSet re) throws SQLException {
				while (re.next()) {
					b.userid = re.getInt(1);
					b.username = re.getString(2);
					b.b_ywm = re.getString(3);
					b.b_zwm = re.getString(4);
					b.zcdz = re.getString(5);
					b.zcsl = re.getBigDecimal(6);
					b.sxf = re.getBigDecimal(7);
					b.sj = re.getTimestamp(8);
					b.zt = EnumParser.parse(ZrType.class, re.getString(9));
					b.id = re.getInt(10);
					b.shsj = re.getTimestamp(11);
					b.ip = re.getString(12);
					b.bid = re.getInt(13);
					b.methodId = re.getString(14);
					b.contractAddress = re.getString(15);
					b.unit = re.getString(16);
					b.cointype = re.getString(17);
				}
				return b;
			}
		}, s.toString(), id);
	}
	
	
	@Override
	public BEntity getBzr(final int id) throws Throwable {
		StringBuffer s = new StringBuffer(
				"SELECT T6010.F01 userid,T6010.F02 username,T6013.F03 ywm,T6013.F04 zwm,");
		s.append(" T6027.F04 zrsl,T6027.F05 sjdz,T6027.F06 zc,T6027.F07 sj,T6027.F08 zr,T6027.F09 sfcg,T6027.F03 bid FROM T6027 ");
		s.append(" LEFT JOIN T6010 ON T6027.F02=T6010.F01");
		s.append(" LEFT JOIN T6013 ON T6027.F03=T6013.F01 WHERE T6027.F01=?");
		return select(getConnection(P2PConst.DB_USER), new ItemParser<BEntity>() {
			BEntity b = new BEntity();

			@Override
			public BEntity parse(ResultSet re) throws SQLException {
				while (re.next()) {
					b.id = id;
					b.userid = re.getInt(1);
					b.username = re.getString(2);
					b.b_ywm = re.getString(3);
					b.b_zwm = re.getString(4);
					b.zrsl = re.getBigDecimal(5);
					b.sjdz = re.getBigDecimal(6);
					b.zr_zcdz = re.getString(7);
					b.sj = re.getTimestamp(8);
					b.zr_zrdz = re.getString(9);
					b.zt = EnumParser.parse(ZrType.class, re.getString(10));
					b.bid = re.getInt(11);
				}
				return b;
			}
		}, s.toString(), id);
	}

	@Override
	public void updateBzc(int id, XlbZcStatus zt, String bz) throws Throwable {
		serviceResource.openTransactions();
		try {
			int accountId = serviceResource.getSession().getAccountId();
			if (id <= 0) {
				throw new ParameterException("指定的记录不存在.");
			}
			if (zt == null) {
				throw new ParameterException("状态不能为空。");
			}
			if (zt == XlbZcStatus.SHSB) {
				zbsb(id);
			}
			execute(getConnection(P2PConst.DB_USER),
					"UPDATE T6028 SET F10=?,F09=?, F11=?,F12=CURRENT_TIMESTAMP() WHERE F01=?", zt, bz, accountId, id);
		} catch (Throwable e) {
			serviceResource.rollback();
			e.printStackTrace();
			throw new LogicalException(e.getMessage());
		}
	}
	
	@Override
	public void updateBzr(int id, ZrType zt, String bz) throws Throwable {
		serviceResource.openTransactions();
		try {
			int accountId = serviceResource.getSession().getAccountId();
			if (id <= 0) {
				throw new ParameterException("指定的记录不存在.");
			}
			if (zt == null) {
				throw new ParameterException("状态不能为空。");
			}
			if (zt.equals(ZrType.S)) {
				execute(getConnection(P2PConst.DB_USER),
						"UPDATE T6027 SET F09=? WHERE F01=?",IsPass.S, id);
				BEntity bzr = getBzr(id);
				int T6025_F01 = selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F02=? AND F03= ?", bzr.userid,
						bzr.bid);
				selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F01=? FOR UPDATE", T6025_F01);// 锁表
				execute(getConnection(P2PConst.DB_USER), "UPDATE T6025 SET F06=F06-?,F04=F04+? WHERE F01=?",
						bzr.sjdz, bzr.sjdz, T6025_F01);
			}else if(zt.equals(ZrType.T)) {
				execute(getConnection(P2PConst.DB_USER),
						"UPDATE T6027 SET F09=? WHERE F01=?",ZrType.T, id);
				BEntity bzr = getBzr(id);
				int T6025_F01 = selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F02=? AND F03= ?", bzr.userid,
						bzr.bid);
				selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F01=? FOR UPDATE", T6025_F01);// 锁表
				execute(getConnection(P2PConst.DB_USER), "UPDATE T6025 SET F06=F06-? WHERE F01=?", bzr.sjdz, T6025_F01);
			}else {
				execute(getConnection(P2PConst.DB_USER),
						"UPDATE T6027 SET F09=? WHERE F01=?",ZrType.TS, id);
			}
			execute(getConnection(P2PConst.DB_USER),
					"INSERT INTO T6027_1 SET F01=?,F02=?,F03=CURRENT_TIMESTAMP(),F04=? ",id, zt, accountId);
		} catch (Throwable e) {
			serviceResource.rollback();
			e.printStackTrace();
			throw new LogicalException(e.getMessage());
		}
	}

	@Override
	public void updateBzcSh(int id, XlbZcStatus zt, String bz) throws Throwable {
		serviceResource.openTransactions();
		try {
			int accountId = serviceResource.getSession().getAccountId();
			if (id <= 0) {
				throw new ParameterException("指定的记录不存在.");
			}
			if (zt == null) {
				throw new ParameterException("状态不能为空。");
			}
			if (zt == XlbZcStatus.ZCCG || zt == XlbZcStatus.QZZC) {
				int userid = 0;
				int bid = 0;
				BigDecimal sl = new BigDecimal(0);
				BigDecimal sxf = new BigDecimal(0);
				IsPass is = null;// 状态
				try (Connection connection = getConnection(P2PConst.DB_USER)) {
					try (PreparedStatement ps = connection
							.prepareStatement("SELECT F02,F03,F05,F06,F08 FROM T6028 WHERE F01=? FOR UPDATE")) {// 锁市场表
						ps.setInt(1, id);
						try (ResultSet re = ps.executeQuery()) {
							while (re.next()) {
								userid = re.getInt(1);
								bid = re.getInt(2);
								sl = re.getBigDecimal(3);
								sxf = re.getBigDecimal(4);
								is = EnumParser.parse(IsPass.class, re.getString(5));
							}
						}
					}
				}
				int T6025_F01 = selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F02=? AND F03= ?", userid,
						bid);

				if (T6025_F01 <= 0) {
					throw new ParameterException("币不存在。");
				}
				if (is == null || is == IsPass.S) {
					throw new ParameterException("已转出。");
				}
				selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F01=? FOR UPDATE", T6025_F01);// 锁表
				execute(getConnection(P2PConst.DB_USER), "UPDATE T6025 SET F05=F05-? WHERE F02=? AND F03=?",
						sl.add(sxf), userid, bid);
				String s = "提币手续费:" + sxf;
				int T6026_F01 = insert(getConnection(P2PConst.DB_USER),
						"INSERT INTO T6026 SET F02=?,F03=?,F04=?,F05=CURRENT_TIMESTAMP(),F06=?,F07=?,F08=?", userid,
						bid, XlbType.TBSXF, sxf, id, s);
				 s = "转出数量：" + sl;;
				execute(getConnection(P2PConst.DB_USER),
						"INSERT INTO T6026 SET F02=?,F03=?,F04=?,F05=CURRENT_TIMESTAMP(),F06=?,F07=?,F08=?", userid,
						bid, XlbType.ZC, sl, id, s);
				BigDecimal zzc = selectBigDecimal(P2PConst.DB_CONSOLE, "SELECT F02 FROM T7015 WHERE F01=? FOR UPDATE",
						bid);// 锁表
				execute(getConnection(P2PConst.DB_CONSOLE),
						"INSERT INTO T7015 SET F01=?,F02=?,F03=?" + " ON DUPLICATE KEY UPDATE F02=F02+?,F03=F03+?", bid,
						sxf, sxf, sxf, sxf);
				execute(getConnection(P2PConst.DB_CONSOLE),
						"INSERT INTO T7016 SET F02=?,F03=?,F04=CURRENT_TIMESTAMP(),F05=?,F06=0,F07=?,F08=?,F09=?,F10=?",
						bid, PtXlbType.TBSXF, sxf, zzc.add(sxf), T6026_F01, bz, userid);
				execute(getConnection(P2PConst.DB_USER), "UPDATE T6028 SET F08=? WHERE F01=?", IsPass.S, id);
				// 发送短信
				String mc = selectString(P2PConst.DB_USER, "SELECT F03 FROM T6013 WHERE F01=?", bid);
				smscg(userid, sl.add(sxf), mc);

			} else {
				zbsb(id);
			}
			execute(getConnection(P2PConst.DB_USER),
					"UPDATE T6028 SET F10=?,F09=?, F13=?,F14=CURRENT_TIMESTAMP() WHERE F01=?", zt, bz, accountId, id);
		} catch (IOException e) {
			serviceResource.rollback();
			e.printStackTrace();
			throw new LogicalException(e.getMessage());
		}
	}

	/**
	 * 发送提币成功短信 2017年8月1日13:47:20
	 * 
	 * @throws IOException
	 */
	private void smscg(int userid, BigDecimal je, String mc) throws SQLException, IOException {
		if (userid > 0) {
			String zcje = Formater.formatAmount_(je, 4);
			ConfigureProvider configureProvider = serviceResource.getResource(ConfigureProvider.class);
			String iphone = selectString(P2PConst.DB_USER, "SELECT F02 FROM T6010 WHERE F01=?", userid);// 发送短信
			String english = "Dear BTB users, you have successfully turned out the SMS_ZC_JE SMS_ZC_BZ, thank you for your use. <br><br>";
			String s = english + configureProvider.getProperty(SystemVariable.SMS_ZC_JE);
			String content = s.replaceAll("SMS_ZC_JE", zcje.toString());
			content = content.replaceAll("SMS_ZC_BZ", mc);
			EmailUtil.sendHtmlMail(iphone,EmailUtil.NIKENAME, content+"<br><br><br><br>", "系统错误", true);
		}
	}

	/**
	 * 发送提币失败通知 2017年8月1日13:47:20
	 * 
	 * @throws IOException
	 */
	private void smssb(int userid, BigDecimal je, String mc) throws SQLException, IOException {
		if (userid > 0) {
			String zcje = Formater.formatAmount_(je, 4);
			ConfigureProvider configureProvider = serviceResource.getResource(ConfigureProvider.class);
			String iphone = selectString(P2PConst.DB_USER, "SELECT F02 FROM T6010 WHERE F01=?", userid);// 发送短信
			String english = "Dear BTB users, your application for transferring SMS_XLBZCBH has not been approved. Please contact customer service for specific reasons. Thank you for your application. <br><br>";
			String s = english + configureProvider.getProperty(SystemVariable.SMS_XLBZCBH);
			String content = s.replaceAll("SMS_XLBZCBH", zcje+" "+mc);
			EmailUtil.sendHtmlMail(iphone,EmailUtil.NIKENAME, content+"<br><br><br><br>", "系统错误", true);
			
		}
	}

	/**
	 * 审核不通过币返回用户账户
	 */
	private void zbsb(int id) throws Throwable {
		if (id <= 0) {
			throw new ParameterException("指定的记录不存在.");
		}
		int userid = 0;
		int bid = 0;
		BigDecimal sl = new BigDecimal(0);
		BigDecimal sxf = new BigDecimal(0);
		XlbZcStatus xlbZcStatus = null;
		try (Connection connection = getConnection(P2PConst.DB_USER)) {
			try (PreparedStatement ps = connection
					.prepareStatement("SELECT F02,F03,F05,F06,F10 FROM T6028 WHERE F01=? FOR UPDATE")) {// 锁市场表
				ps.setInt(1, id);
				try (ResultSet re = ps.executeQuery()) {
					while (re.next()) {
						userid = re.getInt(1);
						bid = re.getInt(2);
						sl = re.getBigDecimal(3);
						sxf = re.getBigDecimal(4);
						xlbZcStatus = EnumParser.parse(XlbZcStatus.class, re.getString(5));
					}
				}
			}
		}
		if (userid <= 0) {
			throw new ParameterException("用户不存在。");
		}
		if (bid <= 0) {
			throw new ParameterException("币不存在。");
		}
		
		if (xlbZcStatus == XlbZcStatus.WSH || xlbZcStatus == XlbZcStatus.SHTG) {
		
			int T6025_F01 = selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F02=? AND F03= ?", userid, bid);
			if (T6025_F01 <= 0) {
				throw new ParameterException("币不存在。");
			}
			selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F01=? FOR UPDATE", T6025_F01);// 锁表
			execute(getConnection(P2PConst.DB_USER), "UPDATE  T6025 SET F04=F04+?,F05=F05-? WHERE F02=? AND F03=?",
					sl.add(sxf), sl.add(sxf), userid, bid);
			// 发送短信
			String mc = selectString(P2PConst.DB_USER, "SELECT F03 FROM T6013 WHERE F01=?", bid);
			smssb(userid, sl.add(sxf), mc);
		
		} else {
			throw new ParameterException("订单已撤销");
		}
	}

	/**
	 * 查询审核人
	 * 
	 * @param userId
	 * @return
	 */
	/*
	 * private String getName(int id) throws SQLException { try
	 * (PreparedStatement pstmt = getConnection(P2PConst.DB_CONSOLE)
	 * .prepareStatement("SELECT F02 FROM T7011 WHERE F01=?")) { pstmt.setInt(1,
	 * id); try (ResultSet resultSet = pstmt.executeQuery()) { if
	 * (resultSet.next()) { return resultSet.getString(1); } } } return ""; }
	 */

	@Override
	public TtQbEntity getPt(int bid) throws Throwable {
		if (bid <= 0) {
			throw new ParameterException("指定的记录不存在.");
		}
		return select(getConnection(P2PConst.DB_CONSOLE), new ItemParser<TtQbEntity>() {

			@Override
			public TtQbEntity parse(ResultSet re) throws SQLException {
				TtQbEntity t = new TtQbEntity();
				while (re.next()) {
					t.dz = re.getString(1);
					t.sy = re.getString(2);
					t.ip = re.getString(3);
					t.prot = re.getString(4);
					t.username = re.getString(5);
					t.pass = re.getString(6);

				}
				return t;
			}
		}, "SELECT F03,F04,F05,F06,F07,F08 FROM T7103 WHERE F02=?", bid);
	}

	@Override
	public void export(BEntity[] bzc, OutputStream outputStream, String charset, XlbZcStatus type) throws Throwable {
		if (outputStream == null) {
			return;
		}
		if (StringHelper.isEmpty(charset)) {
			charset = "GBK";
		}
		if (bzc == null) {
			return;
		}
		if (type == null) {
			type = XlbZcStatus.WSH;
		}
		try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset))) {
			writer.write("用户ID");
			writer.write(",");
			writer.write("用户名");
			writer.write(",");
			writer.write("币种/简称");
			writer.write(",");
			writer.write("转出数量");
			writer.write(",");
			writer.write("手续费");
			writer.write(",");
			writer.write("实际到账");
			writer.write(",");
			writer.write("转出地址");
			writer.write(",");
			if (XlbZcStatus.WSH == type) {
				writer.write("申请时间");
			}
			if (XlbZcStatus.SHTG == type || XlbZcStatus.SHSB == type) {
				writer.write("审核时间");
			}
			if (XlbZcStatus.ZCCG == type) {
				writer.write("转出时间");
			}
			if (XlbZcStatus.ZCSB == type) {
				writer.write("转出审核时间");
			}
			writer.newLine();
			for (BEntity b : bzc) {
				if (b == null) {
					continue;
				}
				writer.write(Integer.toString(b.userid));
				writer.write(",");
				writer.write(b.username == null ? "" : b.username);
				writer.write(",");
				writer.write(b.b_zwm + "/" + b.b_ywm);
				writer.write(",");
				writer.write(format(b.zcsl.add(b.sxf)).toString() + ";");
				writer.write(",");
				writer.write(format(b.sxf).toString() + ";");
				writer.write(",");
				writer.write(format(b.zcsl).toString() + ";");
				writer.write(",");
				writer.write(b.zcdz + "");
				writer.write(",");
				if (XlbZcStatus.WSH == (type)) {
					writer.write(DateTimeParser.format(b.sj, "yyyy-MM-dd HH:mm"));
				}
				if (XlbZcStatus.SHTG == type || XlbZcStatus.SHSB == type) {
					writer.write(DateTimeParser.format(b.shsj, "yyyy-MM-dd HH:mm"));
				}
				if (XlbZcStatus.ZCCG == type) {
					writer.write(DateTimeParser.format(b.zcsj, "yyyy-MM-dd HH:mm"));
				}
				if (XlbZcStatus.ZCSB.name().equals(type)) {
					writer.write(DateTimeParser.format(b.zcsj, "yyyy-MM-dd HH:mm"));
				}
				writer.newLine();
			}
			writer.flush();
		}
	}

	@Override
	public void updateBzcPl(XlbZcStatus zt, String bz, int... ids) throws Throwable {
		for (int id : ids) {
			if (id <= 0) {
				continue;
			}
			serviceResource.openTransactions();
			if (ids == null || ids.length == 0) {
				throw new ParameterException("指定的记录不存在.");
			}
			try {

				int accountId = serviceResource.getSession().getAccountId();
				if (zt == null) {
					throw new ParameterException("状态不能为空。");
				}
				if (zt == XlbZcStatus.SHSB) {
					zbsb(id);
				}
				execute(getConnection(P2PConst.DB_USER),
						"UPDATE T6028 SET F10=?,F09=?, F11=?,F12=CURRENT_TIMESTAMP() WHERE F01=?", zt, bz, accountId,
						id);

			} catch (Throwable e) {
				serviceResource.rollback();
				e.printStackTrace();
				throw new LogicalException(e.getMessage());
			}
		}
	}

	@Override
	public void xlbBd(int id) throws Throwable {
		serviceResource.openTransactions();
		if (id <= 0) {
			throw new ParameterException("指定的记录不存在.");
		}
		try {

			int accountId = serviceResource.getSession().getAccountId();

			int userid = 0;
			int bid = 0;
			BigDecimal sl = new BigDecimal(0);
			BigDecimal sxf = new BigDecimal(0);
			IsPass is = null;// 状态 是否成功
			XlbZcStatus status = null; // 状态
			IsPass sfbd = null;// 是否补过单；
			try (Connection connection = getConnection(P2PConst.DB_USER)) {
				try (PreparedStatement ps = connection
						.prepareStatement("SELECT F02,F03,F05,F06,F08,F10,F15 FROM T6028 WHERE F01=? FOR UPDATE")) {// 锁市场表
					ps.setInt(1, id);
					try (ResultSet re = ps.executeQuery()) {
						while (re.next()) {
							userid = re.getInt(1);
							bid = re.getInt(2);
							sl = re.getBigDecimal(3);
							sxf = re.getBigDecimal(4);
							is = EnumParser.parse(IsPass.class, re.getString(5));
							status = EnumParser.parse(XlbZcStatus.class, re.getString(6));
							sfbd=EnumParser.parse(IsPass.class, re.getString(7));					}
					}
				}
			}
			int T6025_F01 = selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F02=? AND F03= ?", userid, bid);

			if (T6025_F01 <= 0) {
				throw new ParameterException("币不存在。");
			}
			if (is == null || is != IsPass.S) {
				throw new ParameterException("未转出。");
			}
			if (status == null || status != XlbZcStatus.ZCCG) {
				throw new ParameterException("未转出成功。");
			}
			if(sfbd==null||sfbd==IsPass.S){
				throw new ParameterException("已经补过单。");
			}
			selectInt(P2PConst.DB_USER, "SELECT F01 FROM T6025 WHERE F01=? FOR UPDATE", T6025_F01);// 锁表
			execute(getConnection(P2PConst.DB_USER), "UPDATE T6025 SET F04=F04+? WHERE F02=? AND F03=?", sl.add(sxf),
					userid, bid);
			String bz = "补单数量：" + sl.add(sxf);
			int T6026_F01 = insert(getConnection(P2PConst.DB_USER),
					"INSERT INTO T6026 SET F02=?,F03=?,F04=?,F05=CURRENT_TIMESTAMP(),F06=?,F07=?,F08=?", userid, bid,
					XlbType.BD, sl.add(sxf), id, bz);
			BigDecimal zzc = selectBigDecimal(P2PConst.DB_CONSOLE, "SELECT F02 FROM T7015 WHERE F01=? FOR UPDATE", bid);// 锁表
			execute(getConnection(P2PConst.DB_CONSOLE), " UPDATE T7015 SET F02=F02-?,F04=F04+? WHERE F01=?", sxf, sxf,
					bid);
			bz = "补单返还手续费：" + sxf;
			execute(getConnection(P2PConst.DB_CONSOLE),
					"INSERT INTO T7016 SET F02=?,F03=?,F04=CURRENT_TIMESTAMP(),F05=0,F06=?,F07=?,F08=?,F09=?,F10=?",
					bid, PtXlbType.BDFHSXF, sxf, zzc.subtract(sxf), T6026_F01, bz, userid);
			
			execute(getConnection(P2PConst.DB_USER), "UPDATE T6028 SET F15=? WHERE F01=?", IsPass.S, id);
			String content = "补单数量：" + sl.add(sxf) + "币ID：" + bid + "订单ID" + id;
			log(accountId, content);
		} catch (Throwable e) {
			serviceResource.rollback();
			e.printStackTrace();
			throw new LogicalException(e.getMessage());
		}
	}

	@Override
	public void xlbYccl(int id, String hax) throws Throwable {
		if(id<=0){
			throw new ParameterException("ID异常");
		}
		if(StringHelper.isEmpty(hax)){
			throw new ParameterException("描述不能为空。");
		}
		execute(getConnection(P2PConst.DB_USER), "UPDATE T6028 SET F16=? WHERE F01=?", hax,id);
	}
}
